﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using ParamSetCollection.General.Parameters.Alarms;
using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.ParamTable.JTRII.ParamGroup;
using ParamSetCollection.General.Tools;

namespace ParamSetCollection.General.ParamTable.JTRII
{
    public class JTRIITable : TableUnit
    {

        protected void AlarmInfoInit()
        {
            alarmsHelper = new AlarmsHelper();
         
        }

        protected void NomalInfoInit()
        {
            ctxParam = new CTXParam();
            svParam = new SVParam();
            //ctrlParam = new CtrlParam(AddrOffset);
            pvParam = new PVParam();
            inputParam = new InputParam();
            outputParam = new OutputParam();

        }


        public JTRIITable()
            :base()
        {
            AlarmInfoInit();
            NomalInfoInit();

            MachineType = "JTR";
            //PLCType= "MEGMEET";

            CreateFile(MachineType, true);
            ReadParamXMLFile(MachineType);
            ReadAlXMLFile(MachineType);
        }


        private void CreateFile(string machineType,bool test = false)
        {

            if (test == true)//一下为直接生成文档
            {
                string paramType = "";
                string dir1 = System.Environment.CurrentDirectory + "\\Config\\Param\\" + machineType + "\\";

                string path = dir1 + "CTX\\" + "bit.cfg";
                ParamsFileCreateHelper.CreateParamsFile(ctxParam.bitParamDic, typeof(bool), path);
                path = dir1 + "CTX\\" + "short.cfg";
                ParamsFileCreateHelper.CreateParamsFile(ctxParam.shortParamDic, typeof(short), path);
                path = dir1 + "CTX\\" + "DW.cfg";
                ParamsFileCreateHelper.CreateParamsFile(ctxParam.dwParamDic, typeof(Int32), path);


                path = dir1 + "SV\\" + "bit.cfg";
                ParamsFileCreateHelper.CreateParamsFile(svParam.bitParamDic, typeof(bool), path, true);
                path = dir1 + "SV\\" + "short.cfg";
                ParamsFileCreateHelper.CreateParamsFile(svParam.shortParamDic, typeof(short), path, true);
                path = dir1 + "SV\\" + "DW.cfg";
                ParamsFileCreateHelper.CreateParamsFile(svParam.dwParamDic, typeof(Int32), path);


                path = dir1 + "PV\\" + "bit.cfg";
                ParamsFileCreateHelper.CreateParamsFile(pvParam.bitParamDic, typeof(bool), path, true);
                path = dir1 + "PV\\" + "short.cfg";
                ParamsFileCreateHelper.CreateParamsFile(pvParam.shortParamDic, typeof(short), path, true);
                path = dir1 + "PV\\" + "DW.cfg";
                ParamsFileCreateHelper.CreateParamsFile(pvParam.dwParamDic, typeof(Int32), path);


                path = dir1 + "IN\\" + "bit.cfg";
                ParamsFileCreateHelper.CreateParamsFile(inputParam.bitParamDic, typeof(bool), path, true);


                path = dir1 + "OUT\\" + "bit.cfg";
                ParamsFileCreateHelper.CreateParamsFile(outputParam.bitParamDic, typeof(bool), path, true);



            }
            else//正常流程读取文件，增加欠缺字段
            {
  
            }

           
        }

        private void ReadParamXMLFile(string machineType)
        {
            string dir1 = System.Environment.CurrentDirectory + "\\Config\\Param\\" + machineType + "\\";
            string path = dir1 + "CTX\\" + "bit.cfg";
            ReadXMLCfg(path, ctxParam.bitParamDic);
            path = dir1 + "CTX\\" + "short.cfg";
            ReadXMLCfg(path, ctxParam.shortParamDic);
            path = dir1 + "CTX\\" + "DW.cfg";
            ReadXMLCfg(path, ctxParam.dwParamDic);


            path = dir1 + "SV\\" + "bit.cfg";
            ReadXMLCfg(path, svParam.bitParamDic);
            path = dir1 + "SV\\" + "short.cfg";
            ReadXMLCfg(path, svParam.shortParamDic);
            path = dir1 + "SV\\" + "DW.cfg";
            ReadXMLCfg(path, svParam.dwParamDic);


            path = dir1 + "PV\\" + "bit.cfg";
            ReadXMLCfg(path, pvParam.bitParamDic);
            path = dir1 + "PV\\" + "short.cfg";
            ReadXMLCfg(path, pvParam.shortParamDic);
            path = dir1 + "PV\\" + "DW.cfg";
            ReadXMLCfg(path, pvParam.dwParamDic);


            path = dir1 + "IN\\" + "bit.cfg";
            ReadXMLCfg(path, inputParam.bitParamDic);


            path = dir1 + "OUT\\" + "bit.cfg";
            ReadXMLCfg(path, outputParam.bitParamDic);
        }

        private void ReadAlXMLFile(string machineType)
        {
            string dir1 = System.Environment.CurrentDirectory + "\\Config\\Alarm\\" + machineType + "\\";
            string path = dir1 + machineType + ".cfg";

            alarmsHelper.ReadXML(path);
        }

        private void ReadXMLCfg(string path,Dictionary<string,ParameterUnit> puDic)
        {
        
            if (File.Exists(path) == false)
            {
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            List<string> keys = puDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                XmlNode pNode = doc.SelectSingleNode("ParamsList/" + keys[i]);
                if (pNode != null)
                {
                    puDic[keys[i]].ReadXml(pNode);
                }
            }


        }
    }
}
